Week 1 - Inspiring Visualizations¶
This notebook contains some inspiring visualizations that can be quickly created using Python. The goal is to provide you with ideas and examples of how to visualize data effectively. The visualizations are based on real-world datasets and demonstrate various techniques and libraries available in Python for data visualization.
Population Density Map¶
This section is based on the following tutorial: https://medium.com/data-science/creating-beautiful-population-density-maps-with-python-fcdd84035e06 Full credit to the author: Adam Symington (2022) at PythonMaps
In [145]:
# import all relevant libraries
import rasterio
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from matplotlib.colors import ListedColormap
In [146]:
# Import geographical data from the GHS-POP dataset with rasterio
tif_file = rasterio.open('data/GHS_POP_E2030_GLOBE_R2023A_4326_30ss_V1_0/GHS_POP_E2030_GLOBE_R2023A_4326_30ss_V1_0.tif')
ghs_data = tif_file.read()
In [147]:
# Print some metadata
print("Tiff Boundary", tif_file.bounds)
print("Tiff CRS", tif_file.crs)
print("Data shape", ghs_data.shape)
print("Max value", np.amax(ghs_data))
print("Min value", np.amin(ghs_data))
Tiff Boundary BoundingBox(left=-180.00791593130032, bottom=-89.10041610517152, right=180.00874930942342, top=89.0995831776456) Tiff CRS EPSG:4326 Data shape (1, 21384, 43202) Max value 394367.2599414062 Min value 0.0
In [148]:
# Create a custom colormap for the population density
our_cmap = matplotlib.colormaps.get_cmap('hot_r') # Get the continuous 'hot_r' colormap
new_colors = our_cmap(np.linspace(0, 1, 394)) # Sample 394 discrete colors from it
new_cmp = ListedColormap(new_colors) # Create a new discrete colormap
In [149]:
# Create a new figure and axis including resolution (very high resolution, takes very long to render)
fig, ax = plt.subplots(figsize=(20,10), dpi=600)
ax.imshow(ghs_data[0], norm=colors.LogNorm(), cmap=new_cmp) # Create the final plot
ax.axis('off') # Remove axis to make it look cleaner
fig.savefig("pop_density_global.png", dpi=600) # optionally save the figure
plt.show() # Show the figure